package com.honglv.service.impl;


import com.honglv.mapper.RegisterMapper;
import com.honglv.pojo.dto.RegisterDTO;
import com.honglv.pojo.entity.Admin;
import com.honglv.pojo.entity.User;
import com.honglv.pojo.result.Result;
import com.honglv.service.RegisterService;
import com.honglv.utils.RegexUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.Random;

import static com.honglv.constant.MessageConstant.UPDATEPASSWORD_CODE_KEY;

/**
 * @Auther: 生
 * @Date: 2024/04/02/14:32
 * @Description:
 */
@Service
@Slf4j
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private RegisterMapper registerMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 用户&管理员注册
     * @param registerDTO
     * @return
     */
    public Result regiter(RegisterDTO registerDTO) {

        String phone = registerDTO.getPhone();
        //校验手机号
        if(RegexUtil.isPhoneInvalid(phone)){
            //不符合手机号格式  返回错误信息
            return Result.error("手机号格式错误");
        }

        //从redis中获取验证码并且校验 注意LOGIN_CODE_KEY
        String cacheCode = stringRedisTemplate.opsForValue().get(UPDATEPASSWORD_CODE_KEY + phone);
        String code = registerDTO.getIdentifyCode();
        if(cacheCode == null || !cacheCode.equals(code)){
            //验证码不一致报错
            return Result.error("验证码错误");
        }

        Integer flag = 1;//0为管理员用户，1为普通用户
        //判断是否为管理员注册
        Admin inviter = new Admin();
        if(registerDTO.getCode() != null){
            inviter = registerMapper.selectByCode(registerDTO.getCode());
            if(inviter == null){
                return Result.error("邀请码填写错误！");
            }
            flag = 0;
        }
        int ans = 0;
        if(flag == 0){
            // 随机字符集合
            String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            StringBuilder stringBuilder = new StringBuilder();

            // 创建随机数生成器
            Random random = new Random();

            // 循环生成随机字符
            for (int i = 0; i < 5; i++) {
                // 生成随机索引
                int index = random.nextInt(chars.length());
                // 将随机字符添加到字符串生成器中
                stringBuilder.append(chars.charAt(index));
            }
            Admin admin = Admin.builder()
                    .phone(registerDTO.getPhone())
                    .password(registerDTO.getPassword())
                    .code(stringBuilder.toString())
                    .inviterId(inviter.getId())
                    .build();
            ans = registerMapper.saveAdmin(admin);
        }
        if(flag == 1){
            User user = User.builder()
                    .phone(Long.valueOf(registerDTO.getPhone()))
                    .password(registerDTO.getPassword())
                    .build();
            ans = registerMapper.saveUser(user);
        }
        if (ans > 0) {
            return Result.success("注册成功！");
        }else {
            return Result.error("注册失败！");
        }
    }
}
